package io.renren.modules.vastFinance.controller;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.renren.common.utils.Result;
import com.tencent.common.pojo.StatusCode;
import io.renren.modules.vastFinance.entity.CouponEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.vastFinance.entity.UsersEntity;
import io.renren.modules.vastFinance.service.UsersService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 个人信息
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2022-05-20 08:55:30
 */
@RestController
@RequestMapping("crud/user")
public class UsersController {
    @Autowired
    private UsersService userService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("vastFinance:users:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{userId}")
    @RequiresPermissions("vastFinance:users:info")
    public R info(@PathVariable("userId") BigInteger userId){

		UsersEntity users = userService.getById(userId);

        return R.ok().put("users", users);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("vastFinance:users:save")
    public R save(@RequestBody UsersEntity users){
        userService.save(users);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("vastFinance:users:update")
    public R update(@RequestBody UsersEntity users){
        userService.updateById(users);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("vastFinance:users:delete")
    public R delete(@RequestBody Integer[] userIds){
        userService.removeByIds(Arrays.asList(userIds));

        return R.ok();
    }

    /**
     * 获取额度
     * @param userId
     * @return
     */
    @RequestMapping("getAmount")
    public Result<UsersEntity> getAmount(@RequestParam("userId") Integer userId){
        return userService.getAmount(userId);
    }

    /**
     * 获取用户积分
     * @param userId
     * @return
     */
    @RequestMapping("getUserIntegral")
    public Result getUserIntegral(@RequestParam("userId") Integer userId){
        return userService.getUserIntegral(userId);
    }

    /**
     * 减十积分
     */
    @RequestMapping("updateUserIntegral")
    public void updateUserIntegral(@RequestParam("userId") Integer userId){
        userService.updateUserIntegral(userId);
    }


    /**
     * userById
     * @param userId
     * @return
     */
    @RequestMapping("userById")
    public Result<UsersEntity> userById(@RequestParam("userId") Integer userId){
        return userService.userById(userId);
    }

    @RequestMapping("getUserInfo")
    public Result getUserInfo(@RequestParam("userId") String userId) {
        Result result = userService.getUserInfo(userId);
        return result;
    }

    @RequestMapping("uploadImg")
    public Result changeImg(@RequestBody MultipartFile file){
        String user_pic = userService.uploadImg(file);
        return new Result(StatusCode.OK,"新头像上传成功",true,user_pic);
    }

    @RequestMapping("changeHeadImg")
    public Result changeHeadImg(@RequestBody UsersEntity usersEntity) {
        Result result = userService.changeHeadImg(usersEntity);
        return result;
    }

    @RequestMapping("changePhone")
    public Result changePhone(@RequestBody UsersEntity usersEntity) {
        Result result = userService.changePhone(usersEntity);
        return result;
    }

    @RequestMapping("changeNickName")
    public Result changeNickName(@RequestBody UsersEntity usersEntity) {
        Result result = userService.changeNickName(usersEntity);
        return result;
    }

    /**
     * 获取用户签到天数
     * @param userId
     * @return
     */
    @RequestMapping("getUserSingInDays")
    public Result getUserSingInDays(@RequestParam("userId") Integer userId){
        return userService.getUserSingInDays(userId);
    }

    /**
     * 用户签到
     * @param userId
     * @return
     */
    @RequestMapping("cliSign")
    public Result cliSign(@RequestParam("userId") Integer userId){
        return userService.cliSign(userId);
    }


    /**
     * 获取优惠券
     * @param userId
     * @return
     */
    @RequestMapping("getCoupon")
    public Result<List<CouponEntity>> getCoupon(@RequestParam("userId") Integer userId){
        return userService.getCoupon(userId);
    }

    /**
     * 身份证验证
     * @param userId
     * @return
     */
    @RequestMapping("idCardVerify")
    public Result idCardVerify(@RequestParam("userId") Integer userId){
        return userService.idCardVerify(userId);
    }


    /**
     * 放款
     * @param map
     * @param cardNo
     * @param price
     */
    @RequestMapping("loan")
    public void loan(@RequestBody Map<String,Object> map,@RequestParam("cardNo") String cardNo,@RequestParam("price") Integer price){
        userService.loan(map,cardNo,price);
    }

}
